package com.chain.BrotherExchange.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.utils.ActivityBarHelper;

import butterknife.Bind;

/**
 * 币种详情
 */
public class MonetaryDetailsActivity extends BaseActivity {

    @Bind(R.id.freeze_text) TextView mFreezeText;
    @Bind(R.id.balance_text) TextView mBalanceText;
    @Bind(R.id.transfer_layout) View mTransferLayout;
    @Bind(R.id.coin_type_text) TextView mCoinTypeText;
    @Bind(R.id.valuation_text) TextView mValuationText;
    @Bind(R.id.charge_money_layout) View mChargeMoneyLayout;
    @Bind(R.id.extract_money_layout) View mExtractMoneyLayout;

    private String mCoinId;

    public static void launch(
            TextView balanceView, TextView freezeView, TextView valuationView, TextView typeView,
            String coinId, int type) {
        Activity activity = (Activity) balanceView.getContext();
        Pair balancePair = new Pair<>(balanceView, ViewCompat.getTransitionName(balanceView));
        Pair freezePair = new Pair<>(freezeView, ViewCompat.getTransitionName(freezeView));
        Pair valuationPair = new Pair<>(valuationView, ViewCompat.getTransitionName(valuationView));

        ActivityCompat.startActivity(activity,
                new Intent(activity, MonetaryDetailsActivity.class)
                        .putExtra(C.EXTRA_BALANCE, balanceView.getText().toString())
                        .putExtra(C.EXTRA_FREEZE, freezeView.getText().toString())
                        .putExtra(C.EXTRA_VALUATION, valuationView.getText().toString())
                        .putExtra(C.EXTRA_COIN_TYPE, typeView.getText().toString())
                        .putExtra(C.EXTRA_COIN_ID, coinId)
                        .putExtra(C.EXTRA_PAGE_TYPE, type),
                ActivityOptionsCompat.makeSceneTransitionAnimation(activity, balancePair,
                        freezePair, valuationPair).toBundle());
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_monetary_details;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_monetary_details);

        // 用户资产
        mCoinId = getIntent().getStringExtra(C.EXTRA_COIN_ID);
        mFreezeText.setText(getIntent().getStringExtra(C.EXTRA_FREEZE));
        mValuationText.setText(getIntent().getStringExtra(C.EXTRA_VALUATION));
        mBalanceText.setText(getIntent().getStringExtra(C.EXTRA_BALANCE));
        mCoinTypeText.setText(getIntent().getStringExtra(C.EXTRA_COIN_TYPE));

        int pageType = getIntent().getIntExtra(C.EXTRA_PAGE_TYPE, -1);
        if (pageType == C.VALUE_COIN_TYPE) {
            // 充币
            mChargeMoneyLayout.setVisibility(View.VISIBLE);
            mChargeMoneyLayout.setOnClickListener(v ->
                    ChargeMoneyActivity.launch(mBalanceText, mCoinTypeText, mCoinId));

            // 提币
            mExtractMoneyLayout.setVisibility(View.VISIBLE);
            mExtractMoneyLayout.setOnClickListener(v ->
                    ExtractionMonetaryActivity.launch(mBalanceText, mCoinTypeText, mCoinId));
        }

        // 划转
        mTransferLayout.setOnClickListener(v ->
                AssetTransferActivity.launch(mBalanceText, mCoinTypeText, mCoinId, pageType));
    }
}
